
package com.fast.fastenjoy.base.utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

/**
 * 扩展StringUtils类 
 * 
 * <b>修改历史:</b> <br/>
 *  version 1.0.0 2014-6-9 chenliaohua Initial Version <br/>
 */
public class StringExUtils extends StringUtils{

	private static final String SEARCH_STR_$ = "${%s}";
	
	private static final String SEARCH_STR_PLACEHOLDER = "{%s}";
	
	/**
	 * 字符串连接
	 * @param text
	 * @param strs
	 * @return
	 */
	public static String append(String text, String...strs){
		
		if(text == null || strs == null || strs.length == 0){
			return text;
		}
		
		StringBuffer stringBuffer = new StringBuffer(text);
		
		for(String s : strs){
			stringBuffer.append(s);
		}
		
		return stringBuffer.toString();
	}
	
	/**
	 * 替换 “{key}”占位符
	 * @param text
	 * @param key
	 * @param value
	 * @return
	 *            TODO Description <br/>
	 */
	public static String placeholder(String text, String key, String value){
		
		if(StringUtils.isEmpty(key)){
			return text;
		}
		
		return format(text, value, String.format(SEARCH_STR_PLACEHOLDER, key));
	}
	
	/**
	 * 格式化类似于 “${key}”表达式
	 * @param text
	 * @param paramMap
	 * @return
	 */
	public static String $format(String text, Map<String, Object> paramMap){
		
		if(StringUtils.isEmpty(text) || paramMap == null || paramMap.isEmpty()){
			return text;
		}
		
		String result = text;
		
		Iterator<String> iterator = paramMap.keySet().iterator();
		while(iterator.hasNext()){
			String key = iterator.next();
			Object value = paramMap.get(key);
			result = $format(result, key, value == null ? StringUtils.EMPTY : String.valueOf(value));
		}
		
		return result;
	}
	
	/**
	 * 格式化类似于 “${key}”表达式
	 * @param text
	 * @param key
	 * @param value
	 * @return
	 *            TODO Description <br/>
	 */
	public static String $format(String text, String key, String value){
		
		if(StringUtils.isEmpty(key)){
			return text;
		}
		
		return format(text, value, String.format(SEARCH_STR_$, key));
	}
	
	/**
	 * 格式化字符串
	 * @param text
	 * @param replacement
	 * @param searchString
	 * @return
	 */
	public static String format(String text, String replacement, String searchString){
		
		if(isEmpty(text) || isEmpty(searchString)){
			return text;
		}
		
		replacement = replacement == null ? StringUtils.EMPTY : replacement;
		
		return StringUtils.replace(text, searchString, replacement);
	}
	
	/**
     * 查找正则字符串是否包含
     * 
     * @param str
     * @param regex
     * @return
     */
    public static boolean indexOfReg(String str, String regex) {

        if (StringUtils.isEmpty(str) || StringUtils.isEmpty(regex)) {

            return false;
        }

        return str.matches(regex);
    }
    
    /**
     * 字符串数组中获取对应规则的字符，返回第一个匹配的字符
     * 
     * @param transSvcStringCheck
     * @param strs
     * @return
     */
    public static String getStrByArray(StringFilter stringFilter, String... strs) {

    	List<String> resultStr = getResultStrByArray(stringFilter, strs);

        return resultStr != null && !resultStr.isEmpty() ? resultStr.get(0) : null;
    }
    
    /**
     * 字符串数组中获取对应规则的字符索引，返回第一个匹配的字符索引
     * 
     * @param transSvcStringCheck
     * @param strs
     * @return
     */
    public static int getStrIndexByArray(final String regex, String... strs) {

        return getStrIndexByArray(new StringFilter(){
			public boolean checkStr(String s) {
				return s.matches(regex);
			}}, strs);
    }
    
    /**
     * 字符串数组中获取对应规则的字符索引，返回第一个匹配的字符索引
     * 
     * @param transSvcStringCheck
     * @param strs
     * @return
     */
    public static int getStrIndexByArray(StringFilter stringFilter, String... strs) {

    	int index = -1;
    	
    	if (strs != null && strs.length > 0 && stringFilter != null) {
            for (int i = 0,len = strs.length; i < len; i++) {
                if (stringFilter.checkStr(strs[i])) {
                	index = i;
                	break;
                }
            }
        }
    	
        return index;
    }
    
    /**
     * 返回字符串数组中获取对应规则的字符集合
     * 
     * @param transSvcStringCheck
     * @param strs
     * @return
     */
    public static List<String> getResultStrByArray(StringFilter stringFilter, String... strs) {

    	List<String> list = new ArrayList<String>();
    	
        if (strs != null && strs.length > 0 && stringFilter != null) {

            for (String s : strs) {
                if (stringFilter.checkStr(s)) {
                	list.add(s);
                }
            }
        }

        return list;
    }
    
    /**
     * 字符串数组中获取对应规则的字符,返回第一个匹配的字符
     * 
     * @param transSvcStringCheck
     * @param strs
     * @return
     */
    public static String getStrByArray(final String regex, String... strs) {

    	if(regex == null){
    		
    		return null;
    	}
    	
        return getStrByArray(new StringFilter(){
			public boolean checkStr(String s) {
				return s.matches(regex);
			}}, strs);
    }

    /**
     * 比对字符串规则接口
     * 
     * @author chenliaohua
     * 
     */
    public interface StringFilter {
        public boolean checkStr(String s);
    }
	
}
